import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_input_page.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_picker_video.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/ext.dart';
import 'package:enterprise_v6_app/app/modules/application/smart_warehouse/add_products/add_products_controller.dart';
import 'package:flutter/services.dart';

/// 新增产品页面
class AddProductsPage extends StatelessWidget {
  AddProductsPage({super.key});

  final controller = Get.put(AddProductsController());
  /// 获取体积显示
  Widget _getVolumeBox() {
    return Column(
      children: [
        AppGaps.vGap20,
        Row(
          children: [
            Text(
              '*',
              style: TextStyle(
                fontSize: 14.w,
                height: 1.2,
                color: Colors.red,
              ),
            ),
            Text(
              '体积',
              style: TextStyle(
                fontSize: 14.sp,
                height: 1.2,
                fontWeight: FontWeight.w500,
                color: const Color(0xff262F38),
              ),
            ),
            const Spacer(),
            Text(
              controller.productEntity.volume == 0 ? '自动获取' : controller.productEntity.volume.toString(),
              style: TextStyle(
                fontSize: 14.w,
                height: 1.2,
                fontWeight: FontWeight.w400,
                color: controller.productEntity.volume == null ? const Color(0xffA0A7B5) : const Color(0xff262F38),
              ),
            ),
            AppGaps.hGap12,
            Text(
              '㎡',
              style: TextStyle(
                fontSize: 14.w,
                height: 1.2,
                color: const Color(0xff262F38),
              ),
            )
          ],
        ),
        AppGaps.vGap16,
        _getVolumeInputView(hintText: '长度'),
        AppGaps.vGap12,
        _getVolumeInputView(hintText: '宽度'),
        AppGaps.vGap12,
        _getVolumeInputView(hintText: '高度'),
        AppGaps.vGap20,
        Divider(
          height: 1.w,
          color: AppColors.bg_color_grey,
        )
      ],
    );
  }

  /// 产品视角视频

  /// 获取体积输入项
  Widget _getVolumeInputView({required String hintText}) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10.w),
      height: 44.w,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(5.r),
        color: const Color(0xffF7F7FB),
        border: Border.all(
          color: const Color(0xffEBEDF0),
          width: 1.w,
        ),
      ),
      child: Row(
        children: [
          Expanded(
            child: TextField(
              style: TextStyle(
                fontSize: 14.sp,
              ),
              keyboardType: TextInputType.number,
              inputFormatters: [
                FilteringTextInputFormatter.allow(RegExp('[0-9.]')), //只允许输入数字
              ],
              decoration: InputDecoration(
                hintText: '请输入$hintText',
                border: InputBorder.none,
                hintStyle: TextStyle(
                  fontSize: 14,
                  color: Colors.grey[400],
                ),
              ),
              onChanged: (value) {
                if (value.isEmpty) {
                  switch (hintText) {
                    case '长度':
                      controller.productEntity.productLong = 0;
                      break;
                    case '宽度':
                      controller.productEntity.productWidth = 0;
                      break;
                    case '高度':
                      controller.productEntity.productTall = 0;
                      break;
                  }
                } else {
                  switch (hintText) {
                    case '长度':
                      controller.productEntity.productLong = double.parse(value);
                      break;
                    case '宽度':
                      controller.productEntity.productWidth = double.parse(value);
                      break;
                    case '高度':
                      controller.productEntity.productTall = double.parse(value);
                      break;
                  }
                }
                controller.getVolume();
              },
            ),
          ),
          const Text('mm'),
        ],
      ),
    );
  }

  /// 获取产品视角
  Widget _customVideoUploadBox() {
    return Column(
      children: [
        CustomPickerVideo(
          title: '产品视角视频',
          maxCount: 1,
          isRequired: true,
          videoList: controller.productEntity.videoUrl!.isEmpty ? [] : [controller.productEntity.videoUrl!],
          titleTextStyle: TextStyle(
            fontSize: 14.sp,
            height: 1.2,
            fontWeight: FontWeight.w500,
            color: const Color(0xff262F38),
          ),
          isEnabled: true,
          callback: (path) {
            controller.productEntity.videoUrl = path.isNotEmpty ? path.first : '';
          },
        ),
        SizedBox(
          height: 130.w,
          child: Row(
            children: [
              Expanded(
                child: Container(
                    decoration: const BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage(
                      AppImages.icon_three_view_reference,
                    ),
                    // fit: BoxFit.fill,
                  ),
                )),
              ),
              AppGaps.hGap12,
              Expanded(
                  child: Container(
                decoration: BoxDecoration(
                  color: const Color(0xffffffff),
                  border: Border.all(
                    color: const Color(0xffF7F7F7),
                  ),
                  borderRadius: BorderRadius.circular(4.w),
                ),
                child: Column(
                  children: [
                    Container(
                      height: 104.w,
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        color: const Color(0xffE6E6F3),
                        borderRadius: BorderRadius.circular(4.w),
                      ),
                      child: IconButton(
                        icon: Icon(
                          Icons.play_circle,
                          color: Colors.white,
                          size: 30.w,
                        ),
                        onPressed: () {
                          // launchUrl(Uri.parse(Interface.fileUrl + item));
                        },
                      ),
                    ),
                    Expanded(
                        child: Container(
                      alignment: Alignment.center,
                      child: Text('参考视频',
                          style: TextStyle(
                            fontSize: 10.sp,
                            height: 1.2,
                            fontWeight: FontWeight.w500,
                            color: const Color(0xff333333),
                          )),
                    )),
                  ],
                ),
              )),
            ],
          ),
        ),
        AppGaps.vGap12,
        Text('注:平拍、俯拍30°、俯拍60°均需环绕产品一圈进行拍摄且在一个视频中完成!',
            style: TextStyle(
              fontSize: 14.sp,
              height: 1.2,
              fontWeight: FontWeight.w400,
              color: const Color(0xff3074FF),
            ))
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            appBar: CustomAppBar(
              titleText: '新增产品',
            ),
            body: Column(
              children: [
                Container(
                  color: const Color(0xFFF5F5F6),
                  alignment: Alignment.center,
                  height: 12.w,
                ),
                Expanded(
                  child: Container(
                    padding: EdgeInsets.symmetric(horizontal: 16.w),
                    child: SingleChildScrollView(
                      child: Column(
                        children: [
                          CustomInputPage(
                            title: '产品名称',
                            titleFontWeight: FontWeight.w500,
                            text: controller.productEntity.name,
                            onCallBack: (value) {
                              controller.productEntity.name = value;
                            },
                          ),
                          CustomSelectPage(
                              title: '产品类别',
                              titleFontWeight: FontWeight.w500,
                              selectText: controller.productEntity.productCategoryName!,
                              onCallBack: () {
                                controller.showProductCategory();
                              }),
                          CustomSelectPage(
                              title: '产品级别',
                              titleFontWeight: FontWeight.w500,
                              selectText: controller.productEntity.productLevelName!,
                              onCallBack: () {
                                controller.showProductLevel();
                              }),
                          CustomInputPage(
                            title: '组合发数',
                            textInputType: TextInputType.number,
                            titleFontWeight: FontWeight.w500,
                            reg: '[0-9]',
                            text: controller.productEntity.combinationQuantity == 0 ? '' : controller.productEntity.combinationQuantity.toString(),
                            onCallBack: (value) {
                              controller.productEntity.combinationQuantity = value.isNotEmpty ? int.parse(value) : 0;
                            },
                          ),
                          CustomSelectPage(
                              title: '组合类型',
                              titleFontWeight: FontWeight.w500,
                              selectText: controller.productEntity.combinationTypeName!,
                              onCallBack: () {
                                controller.showCombinationType();
                              }),
                          CustomInputPage(
                            title: '单筒药量',
                            textInputType: TextInputType.number,
                            titleFontWeight: FontWeight.w500,
                            suffixText: 'g',
                            text: controller.productEntity.dosage == 0 ? '' : controller.productEntity.dosage.toString(),
                            onCallBack: (value) {
                              controller.productEntity.dosage = value.isNotEmpty ? double.parse(value) : 0;
                            },
                          ),
                          CustomInputPage(
                            title: '箱含量',
                            textInputType: TextInputType.number,
                            titleFontWeight: FontWeight.w500,
                            text: controller.productEntity.boxContent == 0 ? '' : controller.productEntity.boxContent.toString(),
                            onCallBack: (value) {
                              controller.productEntity.boxContent = value.isNotEmpty ? double.parse(value) : 0;
                            },
                          ),
                          CustomInputPage(
                            title: '毛重',
                            suffixText: 'kg',
                            textInputType: TextInputType.number,
                            titleFontWeight: FontWeight.w500,
                            text: controller.productEntity.netWeight == 0 ? '' : controller.productEntity.netWeight.toString(),
                            onCallBack: (value) {
                              controller.productEntity.netWeight = value.isNotEmpty ? double.parse(value) : 0;
                            },
                          ),
                          CustomInputPage(
                            title: '总药量',
                            textInputType: TextInputType.number,
                            titleFontWeight: FontWeight.w500,
                            text: controller.productEntity.totalDosage == 0 ? '' : controller.productEntity.totalDosage.toString(),
                            onCallBack: (value) {
                              controller.productEntity.totalDosage = value.isNotEmpty ? double.parse(value) : 0;
                            },
                          ),
                          CustomSelectPage(
                              title: '燃放效果',
                              titleFontWeight: FontWeight.w500,
                              selectText: controller.productEntity.firingEffectsName!,
                              onCallBack: () {
                                controller.showFiringEffects();
                              }),
                          // 产品体积项
                          _getVolumeBox(),
                          CustomInputPage(
                            title: '燃放者最低年龄要求',
                            textInputType: TextInputType.number,
                            titleFontWeight: FontWeight.w500,
                            reg: '[0-9]',
                            text: controller.productEntity.minimumAgeRequirement == 0 ? '' : controller.productEntity.minimumAgeRequirement.toString(),
                            onCallBack: (value) {
                              controller.productEntity.minimumAgeRequirement = value.isNotEmpty ? int.parse(value) : 0;
                            },
                          ),
                          CustomSelectPage(
                              title: '燃放安全距离',
                              titleFontWeight: FontWeight.w500,
                              selectText: controller.productEntity.safeDistanceName!,
                              onCallBack: () {
                                controller.showSafeDistance();
                              }),
                          CustomSelectPage(
                              title: '存放仓库',
                              titleFontWeight: FontWeight.w500,
                              selectText: controller.productEntity.wareHouseName!,
                              onCallBack: () {
                                controller.showWarehouse();
                              }),
                          AppGaps.vGap10,
                          // 产品视角视频
                          _customVideoUploadBox(),
                          AppGaps.vGap50,
                        ],
                      ),
                    ),
                  ),
                ),
              ],
            ),
            bottomSheet: Container(
              padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 6.w),
              height: 50.w,
              decoration: BoxDecoration(
                color: Colors.white,
                boxShadow: [
                  BoxShadow(
                    color: const Color(0xffBCBCC5).withOpacity(0.3), // 阴影颜色
                    offset: const Offset(0, -5), // 阴影的偏移量，向上偏移，即上方阴影
                    blurRadius: 10.0, // 阴影的模糊半径
                    spreadRadius: 2.0, // 阴影的扩散半径
                  ),
                ],
              ),
              child: Container(
                  height: 40.w,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(color: const Color(0xff5C6DFF), borderRadius: BorderRadius.all(Radius.circular(20.w))),
                  child: Text(
                    '新增',
                    style: TextStyle(
                      fontSize: 16.sp,
                      color: const Color(0xffffffff),
                    ),
                  )).clickOnce(controller.addSubmit),
            ),
          );
        });
  }
}
